/**
 * Copyright (c) 2010-2020 by skzr.org
 * All rights reserved.
 * @Author skzr.org(E-mail:skzr.org@gmail.com)
 * @date 2010-11-1 下午10:55:07
 */
package org.skzr.chart4me.demo.applet;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;

import javax.swing.JApplet;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.plot.XYPlot;
import org.skzr.framework.util.UtilDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 一个股票的JFreeChart例子
 * @jdk 1.6
 * @author skzr.org(E-mail:skzr.org@gmail.com)
 * @version 1.0.0
 */
public class StockJFreeChart extends JApplet {
	private static final long serialVersionUID = 1L;
	protected final transient Logger logger = LoggerFactory
			.getLogger(getClass());

	private void buildChart() {
		final DateAxis dateAxis = new DateAxis("时间");
		final Date date = new Date();
		dateAxis.setRange(UtilDate.getTimeOfDay(date, "9"), UtilDate.getTimeOfDay(date, "15:30"));
		
		//9:00->11:30,13:00->15:00
		SegmentedTimeline timeline = new SegmentedTimeline(1000, (2 * 60 + 30) * 60, (30 + 60) * 60);
		timeline.setStartTime(dateAxis.getMinimumDate().getTime());
		dateAxis.setTimeline(timeline);
		dateAxis.setDateFormatOverride(new DateFormat() {
			private static final long serialVersionUID = 1L;
			private long mid1 = UtilDate.getTimeOfDay(date, "11:30").getTime();
			private long mid2 = UtilDate.getTimeOfDay(date, "13").getTime();
			private DateFormat minFormat = UtilDate.getFormater("HH:mm:ss");
			private DateFormat format = UtilDate.getFormater("HH:mm");
			
			private boolean isMid(Date date) {
				return mid1 == date.getTime() || mid2 == date.getTime();
			}
			@Override
			public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
				if (isMid(date)) return toAppendTo.append("11:30/13:00");
				return (DateTickUnitType.SECOND.equals(dateAxis.getTickUnit().getUnitType()) ? minFormat : format).format(date, toAppendTo, pos);
			}
			
			@Override
			public Date parse(String source, ParsePosition pos) {
				throw new IllegalStateException("[开发者]方法未实现");
			}
		});
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("股票代码", "时间", "价位", null, true, true, true);
		XYPlot plot = chart.getXYPlot();
		plot.setDomainAxis(dateAxis);
		ChartPanel panel = new ChartPanel(chart);
		bindMouseListener(panel);
		add(panel);
	}
	
	private void bindMouseListener(ChartPanel chartPanel) {
		chartPanel.addChartMouseListener(new ChartMouseListener() {
			
			public void chartMouseMoved(ChartMouseEvent event) {
			}
			
			public void chartMouseClicked(ChartMouseEvent event) {
				logger.debug("鼠标点击事件:{}", event);
			}
		});
	}

	@Override
	public void init() {
		logger.info("JFreeChart你好!");
		buildChart();
	}
}
